package com.xudow.app.dynamicstate_old.module.user;

import android.Manifest;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.jude.beam.bijection.RequiresPresenter;
import com.jude.beam.expansion.BeamBaseActivity;
import com.jude.utils.JUtils;
import com.tbruyelle.rxpermissions.RxPermissions;
import com.xudow.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ske on 2016/6/17.
 */
@RequiresPresenter(WriteIntroducePresenter.class)
public class WriteIntroduceActivity extends BeamBaseActivity<WriteIntroducePresenter> {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.rb_group)
    RadioButton rbGroup;
    @BindView(R.id.rb_person)
    RadioButton rbPerson;
    @BindView(R.id.image_face)
    ImageView imageFace;
    @BindView(R.id.image_info)
    ImageView imageInfo;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_introduce_write);
        ButterKnife.bind(this);
        back.setOnClickListener(i -> finish());
        rbGroup.setOnCheckedChangeListener((buttonView, isChecked) -> getPresenter().setType(isChecked ? 1 : 2));
        if ("1".equals(getIntent().getStringExtra("data"))) {
            rbGroup.setChecked(false);
            rbGroup.setEnabled(false);
            rbPerson.setChecked(true);
            rbGroup.setTextColor(Color.GRAY);
            getPresenter().setType(2);
        }
        imageFace.setOnClickListener(i -> {
            RxPermissions.getInstance(this).request(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE)
                    .subscribe(enable -> {
                        if (enable) {
                            new MaterialDialog.Builder(this)
                                    .title("选择图片来源")
                                    .items(new String[]{"拍照", "相册"})
                                    .itemsCallback(new MaterialDialog.ListCallback() {
                                        @Override
                                        public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                                            getPresenter().editFace(which);
                                        }
                                    }).show();
                        } else {
                            JUtils.Toast("请授予权限进行操作");
                        }
                    });

        });
        imageInfo.setOnClickListener(i -> {
            RxPermissions.getInstance(this).request(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE)
                    .subscribe(enable -> {
                        if (enable) {
                            new MaterialDialog.Builder(this)
                                    .title("选择图片来源")
                                    .items(new String[]{"拍照", "相册"})
                                    .itemsCallback(new MaterialDialog.ListCallback() {
                                        @Override
                                        public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                                            getPresenter().editIntro(which);
                                        }
                                    }).show();
                        } else {
                            JUtils.Toast("请授予权限进行操作");
                        }
                    });
        });
        btnSubmit.setOnClickListener(i -> getPresenter().submit());
    }

    public void setFace(Bitmap image) {
        imageFace.setImageBitmap(image);
    }

    public void setInfo(Bitmap image) {
        imageInfo.setImageBitmap(image);
    }

}
